/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package au.edu.swin.swinvehicle.driver;

import au.edu.swin.swinvehicle.driverlicencetype.DriverLicenceTypeEntity;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Administrator
 */
@Stateless
public class DriverManagerBean implements DriverManagerRemote {

    @PersistenceContext
    private EntityManager em;

    public void addDriver(DriverDetailedTO to) throws DriverManagerException {
        try {
            DriverEntity d = new DriverEntity();
            d.setData(to);
            d.setDriverLicenceType(this.em.find(DriverLicenceTypeEntity.class, to.getDriverLicenceType().getId()));
            d.setDeleted(Boolean.FALSE);
            this.em.persist(d);
        } catch (Exception e) {
            e.printStackTrace();
            throw new DriverManagerException("Cannot add driver", e);
        }
    }

    public void editDriver(DriverDetailedTO to) throws DriverManagerException {
        try {
            DriverEntity d = this.em.find(DriverEntity.class, to.getId());
            d.setData(to);
            d.setDriverLicenceType(this.em.find(DriverLicenceTypeEntity.class, to.getDriverLicenceType().getId()));
            this.em.merge(d);
        } catch (Exception e) {
            throw new DriverManagerException("Cannot edit driver", e);
        }
    }

    public void editDriverDetails(DriverDetailedTO to) throws DriverManagerException {
        try {
            DriverEntity d = this.em.find(DriverEntity.class, to.getId());
            d.setDetails(to);
            this.em.merge(d);
        } catch (Exception e) {
            throw new DriverManagerException("Cannot edit driver", e);
        }
    }

    public void deleteDriver(Long id) throws DriverManagerException {
        try {
            DriverEntity d = this.em.find(DriverEntity.class, id);
            d.setDeleted(Boolean.TRUE);
            this.em.merge(d);
        } catch (Exception e) {
            throw new DriverManagerException("Cannot delete driver", e);
        }
    }

    public DriverDetailedTO getDriver(Long id) throws DriverManagerException {
        try {
            DriverEntity d = this.em.find(DriverEntity.class, id);
            return d.getDetailedData();
        } catch (Exception e) {
            throw new DriverManagerException("Cannot get driver by ID " + id.longValue(), e);
        }
    }

    public List<DriverTO> getAllDrivers() throws DriverManagerException {
        try {
            List<DriverTO> results = new ArrayList<DriverTO>();
            Query allDriversQuery = em.createNamedQuery("findAllDrivers");
            List dbResults = allDriversQuery.getResultList();
            for (int i = 0; i < dbResults.size(); i++) {
                DriverEntity d = (DriverEntity) dbResults.get(i);
                results.add(d.getData());
            }
            return results;
        } catch (Exception e) {
            throw new DriverManagerException("Cannot get all drivers", e);
        }
    }

    public List<DriverDetailedTO> getAllDetailedDrivers() throws DriverManagerException {
        try {
            List<DriverDetailedTO> results = new ArrayList<DriverDetailedTO>();
            Query allDriversQuery = this.em.createNamedQuery("findAllDrivers");
            List dbResults = allDriversQuery.getResultList();
            for (int i = 0; i < dbResults.size(); i++) {
                DriverEntity d = (DriverEntity) dbResults.get(i);
                results.add(d.getDetailedData());
            }
            return results;
        } catch (Exception e) {
            throw new DriverManagerException("Cannot get all detailed drivers", e);
        }
    }

    public boolean isUsernameAvailable(String username) throws DriverManagerException {
        try {
            Query findDriverByUsernameQuery = this.em.createNamedQuery("findDriverByUsername");
            findDriverByUsernameQuery.setParameter("username", username);

            List drivers = findDriverByUsernameQuery.getResultList();
            if (drivers != null && drivers.size() != 0) {
                return false;
            } else {
                return true;
            }
        } catch (Exception e) {
            throw new DriverManagerException("Cannot check if the username exist", e);
        }
    }

    /**
     * Authenticate driver using username and password
     * @param username
     * @param password
     * @return Driver ID
     * @throws au.edu.swin.swinvehicle.driver.DriverManagerException
     */
    public Long authenticate(String username, String password) throws DriverManagerException {
        try {
            Query findDriverByUsernameQuery = this.em.createNamedQuery("findDriverByUsername");
            findDriverByUsernameQuery.setParameter("username", username);

            List drivers = findDriverByUsernameQuery.getResultList();
            if (drivers != null && drivers.size() > 0) {
                DriverEntity driver = (DriverEntity) drivers.get(0);

                if (driver.getPassword().equals(password)) {
                    return driver.getId();
                } else {
                    return null;
                }
            } else {
                return null;
            }
        } catch (Exception e) {
            throw new DriverManagerException("Cannot authenticate driver with username " + username, e);
        }
    }
}
